//
//  WYProgrammeController.m
//  Boss
//
//  Created by admin on 2018/7/7.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYProgrammeController.h"
#import "UIButton+CountDown.h"
#import "CZHAddressPickerView.h"
#define kMaxLength 6
@interface WYProgrammeController ()<UITextViewDelegate>

@property (strong, nonatomic) IBOutlet UITextField *nameTextField;

@property (strong, nonatomic) IBOutlet UITextField *moblieTextfield;
@property (strong, nonatomic) IBOutlet UITextField *codeTextField;
@property (strong, nonatomic) IBOutlet UITextField *addressTextfield;

@property (strong, nonatomic) IBOutlet UITextView *LeavingTextView;

@property (strong, nonatomic) IBOutlet UIButton *SirBtn;
@property (strong, nonatomic) IBOutlet UIButton *LadiesAction;
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIButton *tijiao;

@property (strong, nonatomic) IBOutlet UIView *view1;

@property (strong, nonatomic) IBOutlet UIView *view2;
@property (strong, nonatomic) IBOutlet UIView *view3;
@property (strong, nonatomic) IBOutlet UIView *view4;


@property (nonatomic, copy) NSString *province;

@property (nonatomic, copy) NSString *city;

@property (nonatomic, copy) NSString *area;

@property (strong, nonatomic) IBOutlet UIButton *codeBtn;


@property (strong, nonatomic) IBOutlet UILabel *countLabel;


@end

@implementation WYProgrammeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.LeavingTextView setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
    // Do any additional setup after loading the view from its nib.
  //  [self.mainView setCornerRadius:10.f];
    
    
    [self.view1 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
     [self.view2 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
     [self.view3 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
     [self.view4 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
    [self.moblieTextfield addTarget:self action:@selector(numberChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    [self.nameTextField addTarget:self action:@selector(nameChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    [self.codeTextField addTarget:self action:@selector(codeChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    self.addressTextfield.userInteractionEnabled = NO;
    self.codeBtn.userInteractionEnabled = NO;
    self.LeavingTextView.delegate = self;
}
-(void)setupUI
{
    self.view.backgroundColor = [UIColor clearColor];
    self.SirBtn.selected = YES;
    //[self.LeavingTextView setCornerRadius:5.f];
    [self.LeavingTextView setBorderWithWidth:1.f andColor:[UIColor hexStringToColor:@"#dedede" alpha:1.0]];
    
    [self.tijiao setBackgroundColor:kColorMainColor];
    [self setupTextView];
    

    
    
    
}
-(void)textViewDidChange:(UITextView *)textView
{
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    NSString  *nsTextContent = textView.text;
    NSInteger existTextNum = nsTextContent.length;
    if (existTextNum > 50){
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [nsTextContent substringToIndex:50];
        //[textView setAttributedText: [self textViewAttributedStr:s]];
        textView.text = s;
    }
    //不让显示负数
    self.countLabel.text = [NSString stringWithFormat:@"%ld",MAX(0,50 - existTextNum)];
}



-(void)nameChangeed:(UITextField *)textfield
{
    NSString *toBeString = textfield.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; // 键盘输入模
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textfield markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textfield positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > kMaxLength)
            {
                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:kMaxLength];
                if (rangeIndex.length == 1)
                {
                    textfield.text = [toBeString substringToIndex:kMaxLength];
                }
                else
                {
                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, kMaxLength)];
                    textfield.text = [toBeString substringWithRange:rangeRange];
                }
            }
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > kMaxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:kMaxLength];
            if (rangeIndex.length == 1)
            {
                textfield.text = [toBeString substringToIndex:kMaxLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,kMaxLength)];
                textfield.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
}
//    if (textField.text.length >= 6 ) {//输入完成
//        textField.text = [textField.text substringToIndex:6];
//    }
-(void)codeChangeed:(UITextField *)textField
{
    if (textField.text.length >= 6 ) {//输入完成
        textField.text = [textField.text substringToIndex:6];
    }
}

-(void)numberChangeed:(UITextField *)textField
{
    if (textField.text.length >= 11 ) {//输入完成
        textField.text = [textField.text substringToIndex:11];
        [self.codeBtn setTitleColor:kColorMainColor forState:UIControlStateNormal];
        self.codeBtn.userInteractionEnabled = YES;
    }else{
        [self.codeBtn setTitleColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0] forState:UIControlStateNormal];
        self.codeBtn.userInteractionEnabled = NO;
        
        
    }
}



- (void)setupTextView
{
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"留言";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = [UIColor lightGrayColor];
    [placeHolderLabel sizeToFit];
    [self.LeavingTextView addSubview:placeHolderLabel];
    placeHolderLabel.font = [UIFont systemFontOfSize:13.f];
    [self.LeavingTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)getCode:(id)sender {
    UIButton *codeBtn = (UIButton *)sender;
    if (![NSString isPhone:[self.moblieTextfield.text stringByReplacingOccurrencesOfString:@" " withString:@""]]) {
        [self toast:@"请输入正确的手机号"];
        return;
    }
    [codeBtn startWithTime:59 title:@"重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor whiteColor] countColor:kColorMainColor];
    WEAKSELF
    [WYHttpServer sendMessageWithPhone:[self.moblieTextfield.text stringByReplacingOccurrencesOfString:@" " withString:@""] SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        //        if (success) {
        //
        //        }else{
        //            [weakSelf toast:errorMessage];
        //        }
    }];
}
- (IBAction)backAction:(id)sender {
    if (self.backComplete) {
        self.backComplete();
    }
    NSLog(@" wdadsdajdhgahjk");
    
}

- (IBAction)upLoadAction:(id)sender {
    
    if (self.Complete) {
        self.Complete(self.nameTextField.text, self.moblieTextfield.text, self.codeTextField.text, self.addressTextfield.text, self.LeavingTextView.text, self.SirBtn.selected? 1: 0);
    }
    
    
    
}
- (IBAction)gentlemenAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = YES;
    self.LadiesAction.selected = !btn.selected;
}
- (IBAction)LadiesAction:(id)sender {
     UIButton *btn = (UIButton *)sender;
    btn.selected = YES;
    self.SirBtn.selected = !btn.selected;
    
    
}

- (IBAction)selectCityAction:(id)sender {
    WEAKSELF
    [CZHAddressPickerView areaPickerViewWithProvince:self.province city:self.city area:self.area areaBlock:^(NSString *province, NSString *city, NSString *area) {
        // CZHStrongS elf(self);
        weakSelf.province = province;
        weakSelf.city = city;
        weakSelf.area = area;
        
        weakSelf.addressTextfield.text = [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
    }];
    
    
    
    
}




@end
